import { onMounted, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useTableData } from '../../hooks/table/useTableData';
import { dishCategoryDeleteOne, dishCategoryChangeState } from '../../api/dishMgt';
import { useConfirmAction } from '../../hooks/comfirm/useConfirmAction';

export function useDishesTableData() {
  const columns = ref([

    {
      label: '菜品名称',
      prop: 'name',
      align: 'center',
    },
    {
      label: '图片',
      prop: 'image',
      align: 'center',
      isSlot: true,
    },
    {
      label: '菜品分类',
      prop: 'categoryName',
      align: 'center',
    },
    {
      label: '售价',
      prop: 'price',
      align: 'center',
      isSlot: true,
    },
    {
      label: '售卖状态',
      prop: 'status',
      align: 'center',
      isSlot: true,
    },
    {
      label: '最后操作时间',
      prop: 'updateTime',
      align: 'center',
      isSlot: true,
    },
    {
      label: '操作',
      prop: 'general',
      align: 'center',
      isSlot: true,
      width: 200,
    },
  ]);
  const selectedRows = ref([]);
  const { confirm } = useConfirmAction();
  const {
    tableData, getTableData, total, prevClick, nextClick, sizeChange, currentChange,loading
  } = useTableData('/dish/query');
  function handleSelectionChange(selectedRow) {
    selectedRows.value = selectedRow.map((item) => item.id);
  }
  function removeMore() {
    if (selectedRows.value.length === 0) {
      ElMessage.warning('请选择要删除的数据！');
      return false;
    }
    confirm('确认要删除已选中数据吗？', async () => {
      await dishCategoryDeleteOne(selectedRows.value.join(','));
      await getTableData();
    }, () => false);
  }
  function removeOne(row) {
    confirm('确认要删除此数据吗？', async () => {
      await dishCategoryDeleteOne(row.id);
      await getTableData();
    }, () => false);
  }
  // 修改状态
  function changeStatus(row) {
    confirm('确认要进行此操作吗？', async () => {
      await dishCategoryChangeState({ ids: row.id, status: row.status === 1 ? 0 : 1 });
      await getTableData();
    }, () => false);
  }
  // 批量修改状态
  function changeStatusMore(type) {
    if (selectedRows.value.length === 0) {
      ElMessage.warning('请选择要操作的数据！');
      return false;
    }
    confirm('确认要进行此操作吗？', async () => {
      await dishCategoryChangeState({ ids: selectedRows.value.join(','), status: type });
      await getTableData();
    }, () => false);
  }
  onMounted(() => {
    getTableData().then((r) => {});
  });
  return {
    columns,
    tableData,
    total,
    prevClick,
    nextClick,
    sizeChange,
    currentChange,
    handleSelectionChange,
    getTableData,
    removeOne,
    removeMore,
    changeStatus,
    changeStatusMore,
    loading,
  };
}
